/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.table;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.swing.*;

/**
 * Status bar containing buttons to perform common actions.
 *
 * @author Francisco Morero Peyrona
 */

final class StatusBar extends JToolBar
{
   private TbButton       btnFirstRow;
   private TbRepeatButton btnPreviRow;
   private JLabel         lblRecNo;
   private TbRepeatButton btnNextRow;
   private TbButton       btnLastRow;

   private TbButton btnAppend;
   private TbButton btnDelete;

   private TbButton btnZoomIn;
   private TbButton btnZoomOut;
   
   private TbButton btnUpdatable;
   
   private TbButton btnInfo;
   private TbButton btnPrint;
   
   private TbButton btnExport;

   //-------------------------------------------------------------------------//

   StatusBar( ActionListener owner )
   {
      setFloatable( false );
      setRollover( true );

      init();

      add( this.btnFirstRow );
      add( this.btnPreviRow );
      add( this.lblRecNo );
      add( this.btnNextRow );
      add( this.btnLastRow );
      addSeparator( new Dimension( 20,12 ) );
      add( this.btnAppend );
      add( this.btnDelete );
      add( this.btnUpdatable );
      addSeparator( new Dimension( 20,12 ) );
      add( this.btnInfo );
      add( this.btnPrint );
      addSeparator( new Dimension( 20,12 ) );
      add(this.btnExport);
      addSeparator( new Dimension( 20,12 ) );
      add( this.btnZoomIn );
      add( this.btnZoomOut );
      
      // Add owner as listener for all instances of JButton
      Component[] aComp = getComponents();

      for( int n = 0; n < aComp.length; n++ )
      {
         if( aComp[n] instanceof JButton )
            ((JButton) aComp[n]).addActionListener( owner );
      }
   }

   void setUpdatableRowtSet( boolean bUpdateableState, boolean bCanBeUpdateable )
   {
      String sIconName = (bUpdateableState) ? "sb_updatable" : "sb_readonly"; 
      String sToolTip  = "This JDBC driver does not support updatable ResulSets.\n"+
                         "Therefore, you are not allowed to change the data.";
      
      if( bCanBeUpdateable )
          sToolTip  = (bUpdateableState) ? "Data can be modified (click to set 'Read-Only' mode)":
                                           "Data is in 'Read Only' mode (click to set 'Updateable' mode)";
      
      this.btnAppend.setEnabled( bUpdateableState );
      this.btnDelete.setEnabled( bUpdateableState );
      this.btnUpdatable.setIcon( new ImageIcon( getClass().getResource( "images/"+ sIconName +".gif" ) ) );
      this.btnUpdatable.setToolTipText( sToolTip );
      this.btnUpdatable.setEnabled( bCanBeUpdateable );
   }

   void setCurrentRow( int nRow )
   {
      this.lblRecNo.setText( StrWise.padL( String.valueOf( nRow ), 8 )+' ' );
   }
   
   //-------------------------------------------------------------------------//

   private void init()
   {
      this.btnFirstRow = new TbButton(       "sb_first", "Move to first row"   , "FIRST"    );
      this.btnPreviRow = new TbRepeatButton( "sb_previ", "Move to previous row", "PREVIOUS" );

      this.lblRecNo = new JLabel( "       " );
      this.lblRecNo.setOpaque( true );
      this.lblRecNo.setBackground( Color.white );
      this.lblRecNo.setForeground( Color.black );
      this.lblRecNo.setFont( new Font( "Courier", Font.PLAIN, 11 ) );
      this.lblRecNo.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
      this.lblRecNo.setHorizontalTextPosition( SwingConstants.RIGHT );

      this.btnNextRow   = new TbRepeatButton( "sb_next"    , "Move to next row"           , "NEXT"      );
      this.btnLastRow   = new TbButton(       "sb_last"    , "Move to last row"           , "LAST"      );
      
      this.btnAppend    = new TbButton(       "sb_new"     , "Append new empty row"       , "NEW"       );
      this.btnDelete    = new TbButton(       "sb_delete"  , "Delete highlighted row"     , "DELETE"    );
      this.btnUpdatable = new TbButton(       "sb_readonly", ""                           , "UPDATABLE" );
      
      this.btnInfo      = new TbButton(       "sb_info"    , "Shows meta data information", "META_INFO" );      
      this.btnPrint     = new TbButton(       "sb_print"   , "Print table's data"         , "PRINT"     );
      
      this.btnZoomIn    = new TbButton(       "sb_zoom_in" , "Increases font size"        , "ZOOM_IN"   );
      this.btnZoomOut   = new TbButton(       "sb_zoom_out", "Decreases font size"        , "ZOOM_OUT"  );
      
      this.btnExport = new TbButton(			"save"	   , "Export data to File"		  ,	"EXPORT"	);
   }
   
   //-------------------------------------------------------------------------//
   // INNERCLASS: Special ToolBar Button
   //-------------------------------------------------------------------------//
   final class TbButton extends JButton
   {
      TbButton( String sIconName, String sToolTip, String sActionCommand )
      {
         setFocusPainted( false );
         setMargin( new Insets( 0, 0, 0, 0 ) );
         setToolTipText( sToolTip );
         setIcon( new ImageIcon( getClass().getResource( "images/"+ sIconName + ".gif" ) ) );
         setActionCommand( sActionCommand );
      }
   }

   //-------------------------------------------------------------------------//
   // INNERCLASS: Special ToolBar Repeat Button
   //-------------------------------------------------------------------------//
   final class TbRepeatButton extends JRepeatButton
   {
      TbRepeatButton( String sIconName, String sToolTip, String sActionCommand )
      {
         setFocusPainted( false );
         setMargin( new Insets( 0, 0, 0, 0 ) );
         setToolTipText( sToolTip );
         setIcon( new ImageIcon( getClass().getResource( "images/"+ sIconName +".gif" ) ) );
         setActionCommand( sActionCommand );
         setRepeatDelay( 100 );
      }
   }
}